package services;

import domain.entities.InvestigadorProtocoloCentro;
import domain.entities.Involucrado;

import java.util.Collection;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: nico
 * Date: 22/08/12
 * Time: 18:10
 * To change this template use File | Settings | File Templates.
 */
public class CriteriaInvestigator extends Criteria{

    private Involucrado involucrado;

    public CriteriaInvestigator(Involucrado involucrado) {
        this.involucrado = involucrado;
    }

    @Override
    public boolean isIn(Object element) {
        boolean isIn = true;
        Involucrado other = (Involucrado)element;

        isIn &= isSubstring(involucrado.getNombre(), other.getNombre());
        isIn &= isSubstring(involucrado.getApellido(), other.getApellido());
        isIn &= (other.getSpeciality() == null && involucrado.getSpeciality() == null) || (involucrado.getSpeciality() == null) || (involucrado.getSpeciality().equals(other.getSpeciality()));
        isIn &= (other.getTherapeuticArea() == null && involucrado.getTherapeuticArea() == null) || involucrado.getTherapeuticArea() == null || (involucrado.getTherapeuticArea().equals(other.getTherapeuticArea()));
        if(involucrado.getCentro() != null){
            if(other.getCentro() == null && (!other.getCentrosAsPrincipal().isEmpty() || !other.getCentrosAsSub().isEmpty())){
                isIn &= (isInCenter(other.getCentrosAsPrincipal(), involucrado.getCentro().getId()) || isInCenter(other.getCentrosAsSub(), involucrado.getCentro().getId()));
                return isIn;
            }
            isIn &= (involucrado.getCentro() == null ||
                    involucrado.getCentro().getId()== null ||
                    involucrado.getCentro().getId()==-1 ||
                    (other.getCentro() != null &&
                    involucrado.getCentro().getId().equals(other.getCentro().getId())) );
        }
        return isIn;
    }

    private boolean isInCenter(Collection<InvestigadorProtocoloCentro> ipc, int centroId){
        for(InvestigadorProtocoloCentro i : ipc){
            if(i.getProtocoloCentro().getCentro().getId() == centroId) {
                return true;
            }
        }
        return false;
    }

}
